"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 主机表空间目录权限不同，进行创建表空间
Create At   : 2022/04/22
Owner       : @peilinqian
Description :
    1、创建目录权限为000;
    2、创建表空间,主机表空间目录权限为000;
    3、修改目录为100;
    4、创建表空间,主机表空间目录权限为100;
    5、修改目录为200;
    6、创建表空间,主机表空间目录权限为200;
    7、修改目录为300;
    8、创建表空间,主机表空间目录权限为300;
    9、修改目录为400;
    10、创建表空间,主机表空间目录权限为400;
    11、修改目录为500;
    12、创建表空间,主机表空间目录权限为500;
    13、修改目录为600;
    14、创建表空间,主机表空间目录权限为600;
Expect      :
    1、创建目录权限为000;创建成功
    2、创建表空间,主机表空间目录权限为000;创建失败
    3、修改目录为100;修改成功
    4、创建表空间,主机表空间目录权限为100;创建失败
    5、修改目录为200;修改成功
    6、创建表空间,主机表空间目录权限为200;创建失败
    7、修改目录为300;修改成功
    8、创建表空间,主机表空间目录权限为300;创建失败
    9、修改目录为400;修改成功
    10、创建表空间,主机表空间目录权限为400;创建成功
    11、修改目录为500;修改成功
    12、创建表空间,主机表空间目录权限为500;创建成功
    13、修改目录为600;修改成功
    14、创建表空间,主机表空间目录权限为600;创建成功
History     :
    modified：2022/05/18 by pw5328113;简化用例；修改teardown断言信息
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbSysCase269(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta2_dbuser = Node(node='Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.tbspc_name = 'tbspc_dbsys269'
        self.tbspc_path = os.path.join(os.path.dirname(
            macro.DB_INSTANCE_PATH), 'tbspc_dbsys269')
        self.create_sql = f"create tablespace {self.tbspc_name} " \
            f"location '{self.tbspc_path}';"
        self.drop_sql = f"drop tablespace if exists {self.tbspc_name};"
        self.chmod_cmd = f'chmod authority {self.tbspc_path} &&' \
            f'ls -al {os.path.dirname(self.tbspc_path)} ' \
            f'|grep {self.tbspc_name}'

    def test_main(self):
        step_txt = '----step1:创建目录权限为000;expect:创建成功----'
        self.log.info(step_txt)
        mkdir_cmd = f'mkdir {self.tbspc_path}'
        self.log.info(mkdir_cmd)
        mkdir_result = self.pri_dbuser.sh(mkdir_cmd).result()
        self.log.info(mkdir_result)
        chmod_cmd = self.chmod_cmd.replace('authority', '000')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('d---------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step2:创建表空间,主机表空间目录权限为000;expect:创建失败----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.PERMISSION_DENIED, create_result,
                      "执行失败" + step_txt)

        step_txt = '----step3:修改目录为100;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '100')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('d--x------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step4:创建表空间,主机表空间目录权限为100;expect:创建失败----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.PERMISSION_DENIED, create_result,
                      "执行失败" + step_txt)

        step_txt = '----step5:修改目录为200;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '200')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('d-w-------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step6:创建表空间,主机表空间目录权限为200;expect:创建失败----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.PERMISSION_DENIED, create_result,
                      "执行失败" + step_txt)

        step_txt = '----step7:修改目录为300;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '300')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('d-wx------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step8:创建表空间,主机表空间目录权限为300;expect:创建失败----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.PERMISSION_DENIED, create_result,
                      "执行失败" + step_txt)

        step_txt = '----step9:修改目录为400;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '400')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('dr--------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step10:创建表空间,主机表空间目录权限为400;expect:创建成功----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_TBLSPACE_SUCCESS_MSG,
                      create_result, "执行失败" + step_txt)

        step_txt = '----删除表空间;expect:删除成功----'
        self.log.info(step_txt)
        drop_result = self.pri_sh.executDbSql(self.drop_sql)
        self.log.info(drop_result)
        self.assertIn(self.constant.drop_tablespace_success_msg, drop_result,
                      "执行失败" + step_txt)

        step_txt = '----step11:修改目录为500;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '500')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('dr-x------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step12:创建表空间,主机表空间目录权限为500;expect:创建成功----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_TBLSPACE_SUCCESS_MSG,
                      create_result, "执行失败" + step_txt)

        step_txt = '----删除表空间;expect:删除成功----'
        self.log.info(step_txt)
        drop_result = self.pri_sh.executDbSql(self.drop_sql)
        self.log.info(drop_result)
        self.assertIn(self.constant.drop_tablespace_success_msg, drop_result,
                      "执行失败" + step_txt)

        step_txt = '----step13:修改目录为600;expect:修改成功----'
        self.log.info(step_txt)
        chmod_cmd = self.chmod_cmd.replace('authority', '600')
        chmod_result = self.com.get_sh_result(self.pri_root, chmod_cmd)
        self.assertIn('drw-------', chmod_result, "执行失败" + step_txt)

        step_txt = '----step14:创建表空间,主机表空间目录权限为600;expect:创建成功----'
        self.log.info(step_txt)
        create_result = self.pri_sh.executDbSql(self.create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_TBLSPACE_SUCCESS_MSG,
                      create_result, "执行失败" + step_txt)

    def tearDown(self):
        self.log.info("----this is tearDown----")
        step1_txt = '----清理表空间; expect:成功----'
        self.chmod_cmd.replace('authority', '700')
        self.log.info(step1_txt)
        clean_result = self.pri_sh.executDbSql(self.drop_sql)
        self.log.info(clean_result)
        step2_txt = '----删除表空间路径; expect:成功----'
        self.log.info(step2_txt)
        del_cmd = f"rm -rf {self.tbspc_path}; " \
            f"if [ -d {self.tbspc_path} ]; then echo 'exists'; " \
            f"else echo 'not exists'; fi;"
        self.log.info(del_cmd)
        del_result1 = self.pri_dbuser.sh(del_cmd).result()
        del_result2 = self.sta1_dbuser.sh(del_cmd).result()
        del_result3 = self.sta2_dbuser.sh(del_cmd).result()
        self.log.info(del_result1)
        self.log.info(del_result2)
        self.log.info(del_result3)

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
        self.assertIn(self.constant.drop_tablespace_success_msg, clean_result,
                      "执行失败" + step1_txt)
        self.assertEqual('not exists', del_result1, "执行失败" + step2_txt)
        self.assertEqual('not exists', del_result2, "执行失败" + step2_txt)
        self.assertEqual('not exists', del_result3, "执行失败" + step2_txt)
